//
//  TF_Pull_upView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/3.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF_Pull_upView: TF_TopRightOrCornerTopLeft  {
//    @objc public var isShow:Bool = false
    let line = UIView()
    let titleScrollView = UIScrollView()
    let titleBtnArray = NSMutableArray()
    
    let wkWebScrollView = UIScrollView()
    let wkWebViewArray = NSMutableArray()

    override init(frame: CGRect) {
        super.init(frame: frame)
        //线

        self.line.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 239/255.0, alpha: 1.0)
        self.addSubview(self.line)
  
        
        self.titleScrollView.isPagingEnabled = true
        self.titleScrollView.showsVerticalScrollIndicator = false
        self.titleScrollView.showsHorizontalScrollIndicator = false
//        self.titleScrollView.delegate = self;
        self.addSubview(self.titleScrollView)
        
  
        self.wkWebScrollView.isPagingEnabled = true;
        self.wkWebScrollView.isScrollEnabled = false
        self.addSubview(self.wkWebScrollView)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.titleScrollView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 49.5)
       
        self.line.frame = CGRect(x: 0, y: self.titleScrollView.frame.maxY, width: self.frame.size.width, height: 0.5)
        
        self.wkWebScrollView.frame = CGRect(x: 0, y: self.line.frame.maxY, width: self.frame.size.width, height: self.frame.size.height-self.line.frame.maxY)
     
        
    }
    @objc func btnClick(btn:TF_TitleButton)
  
    {
        for i in stride(from: 0, to: self.titleBtnArray.count ,by: 1) {
            let view = self.titleBtnArray[i] as! TF_TitleButton
            view.setTitleColor(UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1.0), for: .normal)
            //灰色
            
             view.Underline.isHidden = true
        }
        //蓝色
        btn.setTitleColor(UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0), for: .normal)
        
        
        btn.Underline.isHidden = false
        let btnWidth:CGFloat = self.frame.size.width
        
        self.wkWebScrollView.setContentOffset(CGPoint(x: Int(btnWidth)*btn.tag, y: 0), animated: true)
       
        
         var t:Int = btn.tag;

         let tabsData = self.wkWebViewArray[t] as! TF_PPTWKWebView
        
        let isurl = TalkfunSharedTools.isWhetherNoUrl(tabsData.content as String)
        
        if (isurl) {
            TalkfunSharedTools.openUrl(str: tabsData.content as NSString)
        }

        
        let i:Int = Int(self.frame.size.width)/90
            t = t+1
   
        if (t>=i) {
            if (t==self.wkWebViewArray.count) {
                self.titleScrollView.contentOffset = CGPoint(x: 90*(self.wkWebViewArray.count-i), y: 0)
                
            } else {
                self.titleScrollView.contentOffset = CGPoint(x:90*(t-i+1), y: 0)
            }
        } else {
            self.titleScrollView.contentOffset =  CGPoint(x:0, y: 0)
    
        }
        
    }
    
   @objc public func destroy()
    {
        for i in stride(from: 0, to: self.wkWebViewArray.count ,by: 1) {
        let view = self.wkWebViewArray[i] as! TF_PPTWKWebView
            view.removeFromSuperview()

        }
        self.wkWebViewArray.removeAllObjects()

    }
    @objc public func getPPTVIew()->(UIView?)
    {
    
        for i in stride(from: 0, to: self.wkWebViewArray.count ,by: 1) {
        let view = self.wkWebViewArray[i] as! TF_PPTWKWebView
        
            if (view.type == "courseware") {
                return view.PPTView
            }
            
        }

        return nil;

    }
    @objc public func initUI(tabsDataList:NSMutableArray)

    {
        self.wkWebScrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
      
        let newTabsDataList = NSMutableArray()
        for i in stride(from: 0, to: tabsDataList.count ,by: 1) {
        let tabsData = tabsDataList[i] as! TFtabsData
            //不是聊天  不是榜单
            if (tabsData.type != "chat" && tabsData.type != "invite") {
                newTabsDataList.add(tabsData)
            }
         
        }
        for i in stride(from: 0, to: self.titleBtnArray.count ,by: 1) {
        let view = self.titleBtnArray[i] as! TF_TitleButton

            view.removeFromSuperview()

        }
        self.titleBtnArray.removeAllObjects()

        for i in stride(from: 0, to: self.wkWebViewArray.count ,by: 1) {
        let view = self.wkWebViewArray[i] as! TF_PPTWKWebView
            view.removeFromSuperview()

        }
        self.wkWebViewArray.removeAllObjects()

        
        self.wkWebScrollView.contentSize = CGSize(width: CGFloat(newTabsDataList.count)*self.wkWebScrollView.frame.size.width, height: self.wkWebScrollView.frame.size.height)
        
        
        let width:Int = 90
        for i in stride(from: 0, to: newTabsDataList.count ,by: 1) {

            let tabsData = newTabsDataList[i] as! TFtabsData
            
            let titleButton =  TF_TitleButton()
          
            titleButton.tag = i
            
            titleButton.setTitle(tabsData.title, for: .normal)

            titleButton.titleLabel!.font = UIFont.PingFang(size: 15)

            titleButton.frame = CGRect(x: width*i, y: 0, width: width, height: Int(self.titleScrollView.frame.size.height))

            if (i == 0) {
                
                titleButton.setTitleColor(UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0), for: .normal)
                //蓝色
            
                titleButton.Underline.isHidden = false
            }else{
                //灰色
                titleButton.setTitleColor(UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1.0), for: .normal)
               
                titleButton.Underline.isHidden = true
            }
            titleButton.addTarget(self, action: #selector(self.btnClick(btn:)), for: .touchUpInside)
            
            self.titleScrollView.addSubview(titleButton)
            self.titleBtnArray.add(titleButton)
  

            
            let WebView = TF_PPTWKWebView()
            WebView.frame = CGRect(x: self.wkWebScrollView.frame.size.width * CGFloat(i), y: 0, width: self.wkWebScrollView.frame.size.width, height: self.wkWebScrollView.frame.size.height)
     
          
            self.wkWebScrollView.addSubview(WebView)
            self.wkWebViewArray.add(WebView)
          
            
            WebView.tag = i
            WebView.type = tabsData.type as NSString;
            //课件
            if (WebView.type == "courseware") {
                WebView.coursewareUI()
              
            }
            //自定义链接
          else if (WebView.type == "link") {
              WebView.linkUI(content: tabsData.content as NSString)

            }else if (WebView.type == "editor") {
            //自定义图文
                WebView.editorUI(msg: tabsData.content as NSString)
//                [WebView editorUIWithMsg:tabsData.content];
          //  [WebView editorUI:tabsData.content];
              
            }
      
        }

        if (width*newTabsDataList.count > Int(self.frame.size.width)) {
            self.titleScrollView.contentSize = CGSize(width: width*newTabsDataList.count, height: 0)
        } else {
            self.titleScrollView.contentSize = CGSize(width: Int(self.frame.size.width), height: 0)  //CGSizeMake(self.frame.size.width, 0);
        }

    }
    @objc public func isShow(value:Bool)
    {
        UIView.animate(withDuration: 0.5) {
            
       let pull_upViewHight:CGFloat = 397
            if (value) {
                
                self.frame = CGRect(x: 0, y: self.superview!.frame.size.height - pull_upViewHight, width: self.superview!.frame.size.width, height: pull_upViewHight)
                //CGRectMake(0, self.superview.frame.size.height - pull_upViewHight, self.superview.frame.size.width, pull_upViewHight);
            }else{
               
                self.frame = CGRect(x: 0, y: self.superview!.frame.size.height, width: self.superview!.frame.size.width, height: pull_upViewHight)
               // CGRectMake(0, self.superview.frame.size.height, self.superview.frame.size.width, pull_upViewHight);
            }
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
